package logic

import (
	"context"
	"smsbridge/models/cache"
	"smsbridge/smsrpc/internal/svc"
	"smsbridge/smsrpc/smsrpc"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
	"k8s.io/apimachinery/pkg/util/rand"
)

type SmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SmsLogic {
	return &SmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SmsLogic) Sms(in *smsrpc.SmsRequest) (*smsrpc.SmsResponse, error) {
	// todo: add your logic here and delete this line
	//一分钟发送两次后不允许再次发送

	code := rand.Intn(9000) + 1000
	//pkg2.Sms(in.Moble, strconv.Itoa(code))
	cache.Rdb.Set(l.ctx, "smsTwo"+in.Moble, code, time.Minute*3)
	//i, _ := cache.Rdb.Get(l.ctx, "smsTwo"+in.Moble).Int64()
	//if i >= 1 {
	//	return nil, errors.New("你一发送两次不能再发")
	//}
	//
	//result, _ := cache.Rdb.Incr(l.ctx, "smsTwo"+in.Moble).Result()
	//if result == 2 {
	//	cache.Rdb.Set(l.ctx, "smsTwo"+in.Moble, 2, time.Minute*1)
	//}
	return &smsrpc.SmsResponse{}, nil
}
